<template>
  <ele-split-layout
    width="400px"
    space="5px"
    :right-style="{ overflow: 'hidden' }"
    :style="{ minHeight: 'calc(100vh - 88px)' }"
  >
    <!-- 表格 -->
    <VxeBasicTable ref="TableRef" v-bind="options" v-on="gridEvents">
      <template #toolbar_buttons>
        <ToolBtn
          :create-call="openEdit"
          :delete-call="remove"
          :show-export="false"
          :show-import="false"
          :show-delete="false"
        />
      </template>
      <template #action="{ row }">
        <Action :record="row" @edit="openEdit" @delete="remove" />
      </template>
    </VxeBasicTable>
    <template #content>
      <dict-data :dict-id="current.id" />
    </template>
    <!-- 编辑弹窗 -->
    <dict-edit v-model:visible="showEdit" :data="editData" @done="reload" />
  </ele-split-layout>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { message } from 'ant-design-vue/es';
  import { messageLoading } from 'ele-admin-pro/es';
  import DictData from './components/dict-data.vue';
  import DictEdit from './components/dict-edit.vue';
  import { useConfig } from './config';
  import { pageDictionaries, removeDictionary } from '@/api/system/dictionary';
  import type { Dictionary } from '@/api/system/dictionary/model';
  import { VxeGridListeners } from '@/components/VxeTable';

  // 表格实例
  const { TableRef, ToolBtn, Action, options } = useConfig(pageDictionaries);

  const gridEvents: VxeGridListeners = {
    cellClick({ row }) {
      current.value = row;
    }
  };
  // 表格选中数据
  const current = ref<Dictionary>({ id: 0 });

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  // 编辑回显数据
  const editData = ref<Dictionary | null>(null);

  /* 表格渲染完成回调 */
  // const done: EleProTableDone<Dictionary> = (res) => {
  //   if (res.data?.length) {
  //     current.value = res.data[0];
  //   }
  // };

  /* 刷新表格 */
  const reload = () => {
    TableRef.value?.commitProxy('query');
  };

  /* 打开编辑弹窗 */
  const openEdit = (row?: Dictionary | null) => {
    editData.value = row ?? null;
    showEdit.value = true;
  };

  /* 删除 */
  const remove = () => {
    const hide = messageLoading('请求中..', 0);
    removeDictionary(current.value?.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };

  /* 行点击事件 */
  // const customRow = (record: Dictionary) => {
  //   return {
  //     onClick: () => {
  //       current.value = record;
  //     }
  //   };
  // };
</script>

<script lang="ts">
  export default {
    name: 'SystemDictionary'
  };
</script>

<style lang="less" scoped>
  .sys-dict-table {
    :deep(.ant-table-body) {
      overflow: auto !important;
      overflow: overlay !important;
    }

    :deep(.ant-table-row) {
      cursor: pointer;
    }
  }
</style>
